﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NotepadSharp.IO
{
    class FileExplorer
    {
        public bool CreateRoot(TreeView treeView)
        {
            bool returnValue = false;

            try
            {
                // Create Desktop
                TreeNode desktop = new TreeNode();
                desktop.Text = "Desktop";
                desktop.Tag = "Desktop";
                desktop.Nodes.Add("");
                desktop.ImageIndex = desktop.SelectedImageIndex = 2;
                treeView.Nodes.Add(desktop);
                // Get driveInfo
                foreach (DriveInfo drv in DriveInfo.GetDrives())
                {
                    TreeNode fChild = new TreeNode();

                    int imageIndex = 0;

                    if (drv.Name.Equals(@"C:\", StringComparison.CurrentCultureIgnoreCase) && drv.DriveType == DriveType.Fixed)
                    {
                        imageIndex = 3;
                    }
                    else
                    {
                        switch (drv.DriveType)
                        {
                            case DriveType.Fixed:
                                imageIndex = 4;
                                break;
                            case DriveType.CDRom:
                                imageIndex = 5;
                                break;
                            case DriveType.Network:
                                imageIndex = 6;
                                break;
                        }
                    }
                    fChild.ImageIndex = fChild.SelectedImageIndex = imageIndex;
                    fChild.Text = drv.Name;
                    fChild.Nodes.Add("");
                    treeView.Nodes.Add(fChild);
                    returnValue = true;
                }
            }
            catch (Exception)
            {
                returnValue = false;
            }
            return returnValue;
        }

        public TreeNode EnumerateDirectory(TreeNode parentNode)
        {
            try
            {
                DirectoryInfo rootDir;
                Char[] arr = { '\\' };
                string[] nameList = parentNode.FullPath.Split(arr);

                if (nameList.GetValue(0).ToString() == "Desktop")
                {
                    string path = Environment.SpecialFolder.Desktop + "\\";
                    for (int i = 1; i < nameList.Length; i++)
                    {
                        path = path + nameList[i] + "\\";
                    }
                    rootDir = new DirectoryInfo(path);
                }
                else
                {
                    rootDir = new DirectoryInfo(parentNode.FullPath + "\\");
                }

                parentNode.Nodes[0].Remove();
                foreach (DirectoryInfo dir in rootDir.GetDirectories())
                {
                    TreeNode node = new TreeNode();
                    node.Text = dir.Name;
                    node.ImageIndex = node.SelectedImageIndex = 1;
                    node.Nodes.Add("");
                    parentNode.Nodes.Add(node);
                }
                //Fill files
                foreach (FileInfo file in rootDir.GetFiles())
                {
                    TreeNode node = new TreeNode();
                    node.Text = file.Name;
                    node.ImageIndex = node.SelectedImageIndex = 0;
                    parentNode.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                //TODO : 
            }
            return parentNode;
        }
    }
}
